"""
汽车加油记录数据库模型
"""

from datetime import UTC, datetime

from sqlalchemy import DateTime, Integer, Numeric, String
from sqlalchemy.orm import Mapped, mapped_column

from src.database import Base


class GasStationType:
    """加油站类型常量"""

    SINOPEC = "sinopec"  # 中石化
    PETROCHINA = "petrochina"  # 中石油
    CNOOC = "cnooc"  # 中海油
    SHELL = "shell"  # 壳牌
    EXXONMOBIL = "exxonmobil"  # 埃克森美孚
    OTHER = "other"  # 其他

    @classmethod
    def get_all_types(cls):
        """获取所有加油站类型"""
        return [
            cls.SINOPEC,
            cls.PETROCHINA,
            cls.CNOOC,
            cls.SHELL,
            cls.EXXONMOBIL,
            cls.OTHER,
        ]

    @classmethod
    def validate_type(cls, station_type: str) -> bool:
        """验证加油站类型是否有效"""
        return station_type in cls.get_all_types()


class Vehicle(Base):
    """车辆模型"""

    __tablename__ = "vehicles"

    # 基本信息
    family_id: Mapped[int] = mapped_column(
        Integer, nullable=False, comment="家庭ID,用于区分不同家庭"
    )
    family_name: Mapped[str] = mapped_column(
        String(50), nullable=False, comment="家庭名称"
    )
    name: Mapped[str] = mapped_column(String(100), nullable=False, comment="车辆名称")
    brand: Mapped[str] = mapped_column(String(50), nullable=False, comment="品牌")
    model: Mapped[str] = mapped_column(String(50), nullable=False, comment="型号")
    license_plate: Mapped[str] = mapped_column(
        String(20), nullable=False, comment="车牌号"
    )
    year: Mapped[int | None] = mapped_column(Integer, nullable=True, comment="年份")

    # 车辆状态
    is_active: Mapped[bool] = mapped_column(
        Integer, nullable=False, default=1, comment="是否活跃"
    )

    # 备注信息
    notes: Mapped[str | None] = mapped_column(
        String(500), nullable=True, comment="备注信息"
    )

    def __repr__(self):
        return f"<Vehicle(id={self.id}, name='{self.name}', license_plate='{self.license_plate}', family_id={self.family_id})>"


class VehicleFuelRecord(Base):
    """汽车加油记录模型"""

    __tablename__ = "vehicle_fuel_records"

    # 基本信息
    family_id: Mapped[int] = mapped_column(
        Integer, nullable=False, comment="家庭ID,用于区分不同家庭"
    )
    family_name: Mapped[str] = mapped_column(
        String(50), nullable=False, comment="家庭名称"
    )
    vehicle_id: Mapped[int] = mapped_column(Integer, nullable=False, comment="车辆ID")
    vehicle_name: Mapped[str] = mapped_column(
        String(100), nullable=False, comment="车辆名称"
    )
    current_mileage: Mapped[float] = mapped_column(
        Numeric(10, 2), nullable=False, comment="当前里程(公里)"
    )
    fuel_amount: Mapped[float] = mapped_column(
        Numeric(8, 2), nullable=False, comment="本次加油量(升)"
    )
    fuel_cost: Mapped[float] = mapped_column(
        Numeric(10, 2), nullable=False, comment="本次加油金额(元)"
    )
    fuel_price: Mapped[float] = mapped_column(
        Numeric(6, 2), nullable=False, comment="本次油价(元/升)"
    )
    gas_station_type: Mapped[str] = mapped_column(
        String(20), nullable=False, comment="加油站类型"
    )

    # 计算字段
    fuel_consumption: Mapped[float | None] = mapped_column(
        Numeric(6, 2), nullable=True, comment="油耗(升/100公里)"
    )

    # 备注信息
    notes: Mapped[str | None] = mapped_column(
        String(500), nullable=True, comment="备注信息"
    )

    # 时间信息
    refuel_date: Mapped[datetime] = mapped_column(
        DateTime, nullable=False, comment="加油日期", default=lambda: datetime.now(UTC)
    )

    def __repr__(self):
        return f"<VehicleFuelRecord(id={self.id}, vehicle_id={self.vehicle_id}, current_mileage={self.current_mileage}, fuel_cost={self.fuel_cost})>"
